 
	
	var status_updaterBox = 0;
	
	function checkForUpdates(){
		var sendData = {
			path: JACKED_UPDATER_REMOTE,
			client: JACKED_CLIENT_VERSION,
			modules: mods
		};
		$.ajax({
			type: "POST",
			url: "jacked_proxy.php",
			data: sendData,
			dataType: "json",
			success: function (data, textStatus) {
				if(data){
					updateStatusBox(data);
				}else{
					$("#updaterStatus").fadeOut("slow", function(){
						$("#updaterStatus").html('<p>There was an error contacting the update server; try again later!</p>')
						.append('<p>No response.</p>')
						.fadeIn("slow");
					});
				}
			},
			error: function(xhr, msg, except){
				$("#updaterStatus").fadeOut("slow", function(){
					$("#updaterStatus").html('<p>There was an error contacting the update server; try again later!</p>')
					.append('<p>' + msg + '</p>')
					.fadeIn("slow");
				});
			}
		});
	}
	
	function downloadUpdate(location, id){
		var sendData = {
			loc: location,
			id: id
		};
		$.ajax({
			type: "POST",
			url: "jacked_updaterDownload.php",
			data: sendData,
			dataType: "json",
			success: function (data, textStatus) {
				updateStatusBox(data);
			},
			error: function(xhr, msg, except){
				$("#updaterStatus").fadeOut("slow", function(){
					$("#updaterStatus").html('<p>There was an error contacting the update server; try again later!</p>')
					.fadeIn("slow");
				});
			}
		});
	}
	
	function updateStatusBox(data){
		$("#updaterStatus").fadeOut("slow", function(){
			if(data.update == true){
				$("#updaterStatus").html('<p>Update available!</p>')
				.append('<p>' + data.newName + '</p>')
				.append('<p>JACKED Version ' + data.newVer + ' [' + data.majorName + ']</p>')
				.append('<p>' + data.desc + '</p>')
				.append('<input type="hidden" id="updateLocation" value="' + data.location + '" />')
				.append('<input type="hidden" id="updateId" value="' + data.id + '" />')
				.append('<p><a id="downloadUpdate">Download this update</a></p>');
			}else if(data.update == false){
				$("#updaterStatus").html('<p>Version ' + JACKED_CLIENT_VERSION + ' is the newest available.</p>');
				if(data.reason == '')
					$("#updaterStatus").append('<p>But, updates are released regularly, so check back soon!</p>');
				else
					$("#updaterStatus").append('<p>' + data.reason + '</p>');
			}
			
			if(data.downloaded == true){
				$("#updaterStatus").html('<p>Download Conplete!</p>')
				.append('<p>The update package has been downloaded to your server. Click the link below to begin the install process.</p>')
				.append('<p><a id="installUpdate">Install this update</a></p>');
			}else if(data.downloaded == false){
				$("#updaterStatus").html('<p>Download Error!</p>')
				.append('<p>Sorry, but there was a problem downloading the update package. Try again or contact support.</p>');
			}
			bindClicks();
			$("#updaterStatus").fadeIn("slow");
		});
	}
	
	function bindClicks(){
		
		//bind to the click event of the button
		$("#startUpdater").bind("click", function(){
			$("#updaterBox").animate({ 
					height: "400px"
				}, 600);
			$("#updaterStatus").fadeOut("slow", function(){
				$("#updaterStatus").html('<p>Checking for updates to version ' + JACKED_CLIENT_VERSION + '...</p>')
				.append('<div class="thinking">&nbsp;</div>')
				.fadeIn("slow");
				checkForUpdates();
			});
			
		});
		
		$("#downloadUpdate").bind("click", function(){
			var loc = $("#updateLocation").val();
			var id = $("#updateId").val();
			$("#updaterStatus").fadeOut("slow", function(){
				$("#updaterStatus").html('<p>Downloading update package...</p>')
				.append('<div class="thinking">&nbsp;</div>')
				.fadeIn("slow");
				downloadUpdate(loc, id);
			});
			
		});
		
		$("#installUpdate").bind("click", function(){
			$("#updaterStatus").fadeOut("slow", function(){
				$("#updaterStatus").html('<p>Unpacking update package...</p>')
				.append('<div class="thinking">&nbsp;</div>')
				.append('<p>This page will reload when the package installer is being run!</p>')
				.fadeIn("slow");
				$("#updaterStatus").animate({
					height: "400px"
				}, 3000, function(){
					window.location.reload();
				});
			});
			
		});
		
	}
	
	//document ready
	$("document").ready(function(){
		
		bindClicks();
		
	});